﻿using System;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
using fps_arrow.Properties;
using System.Runtime.InteropServices;
using System.Configuration;
using System.Configuration;
using System.IO;

namespace fps_arrow
{
    public partial class Form1 : Form
    {
        const string UrlSourceCode = "https://gitee.com/tidys/FPS-Arrow";
        const string UrlDoc = "https://gitee.com/tidys/FPS-Arrow/blob/master/doc/Use.md";
        const string UrlQQ = "http://wpa.qq.com/msgrd?v=3&uin=774177933&site=qq&menu=yes";

        private Arrow arrowWin = null;
        public Form1()
        {
            InitializeComponent();
            this.UpdateToolTip();
            this.ReadConfig();
            // for auto
            if (false)
            {
                this.CreateArrowWindow();
                this.WindowState = FormWindowState.Minimized;
            }
        }
        private void ReadConfig()
        {
            string configFilePath = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
            Console.WriteLine("config path: " + configFilePath);
            // 从配置中读取
            this.offsetX.Value = Settings.Default.OffsetX;
            this.offsetY.Value = Settings.Default.OffsetY;
            this.opacity.Value = Settings.Default.Opacity;
            this.colorButton.BackColor = Settings.Default.Color;

            this.lineWidth.Value = Settings.Default.LineWidth;
            this.lineLen.Value = Settings.Default.LineLen;
            this.lineSpace.Value = Settings.Default.LineSpace;

            this.borderLineDistance.Value = Settings.Default.BorderLineDistance;
            this.borderLineWidth.Value = Settings.Default.BorderLineWidth;
            this.borderLineLen.Value = Settings.Default.BorderLineLen;

            this.pointRadius.Value = Settings.Default.PointRadius;
            this.pointFill.Checked = Settings.Default.PointFill;
            this.checkBoxUseSvg.Checked = Settings.Default.UseSvg;
            int pointWidth = Settings.Default.PointWidth;
            if (pointWidth <= 1)
            {
                pointWidth = 1;
            }
            this.pointWidth.Value = pointWidth;
        }
        private void SaveConfig()
        {
            Settings.Default.OffsetX = (int)this.offsetX.Value;
            Settings.Default.OffsetY = (int)this.offsetY.Value;
            Settings.Default.Opacity = this.opacity.Value;
            Settings.Default.Color = this.colorButton.BackColor;

            Settings.Default.LineLen = (int)this.lineLen.Value;
            Settings.Default.LineWidth = (int)this.lineWidth.Value;
            Settings.Default.LineSpace = (int)this.lineSpace.Value;

            Settings.Default.BorderLineLen = (int)this.borderLineLen.Value;
            Settings.Default.BorderLineWidth = (int)this.borderLineWidth.Value;
            Settings.Default.BorderLineDistance = (int)this.borderLineDistance.Value;

            Settings.Default.PointRadius = (int)this.pointRadius.Value;
            Settings.Default.PointWidth = (int)this.pointWidth.Value;
            Settings.Default.PointFill = this.pointFill.Checked;
            Settings.Default.UseSvg = this.checkBoxUseSvg.Checked;

            Settings.Default.Save();
        }

        private void OnRunButtonClick(object sender, EventArgs e)
        {
            if (this.arrowWin != null)
            {
                this.CloseArrowWindow();
            }
            else
            {
                this.SaveConfig();
                this.CreateArrowWindow();
            }
        }

        private void onSourceCodeLinkClick(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.OpenUrl(UrlSourceCode);
        }
        private void OnUseDocClick(object sender, EventArgs e)
        {
            this.OpenUrl(UrlDoc);
        }

        private void OpenUrl(string url)
        {
            System.Diagnostics.Process.Start("iexplore.exe", url);
        }
        private void CreateArrowWindow()
        {
            this.CloseArrowWindow();
            this.arrowWin = new Arrow();
            this.arrowWin.Show();
            this.runButton.Text = "关闭准星(ESC)";
        }
        private void CloseArrowWindow()
        {
            if (this.arrowWin != null)
            {
                this.arrowWin.Close();
                this.arrowWin = null;
                this.runButton.Text = "创建准星";
            }
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                this.CloseArrowWindow();
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }



        private void onAppTimer(object sender, EventArgs e)
        {
            this.TopMost = true;
        }

        private void OnCloseApp(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
            Console.WriteLine(config.FilePath);
        }


        private void NotifyRefreshArrowWindow()
        {
            if (this.arrowWin != null)
            {
                this.SaveConfig();
                this.arrowWin.OnRefresh();
            }
        }

        private void OnValueChanged(object sender, EventArgs e)
        {
            this.NotifyRefreshArrowWindow();
        }

        private void colorButton_Click(object sender, EventArgs e)
        {
            DialogResult result = this.colorDialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                this.colorButton.BackColor = this.colorDialog.Color;
                this.NotifyRefreshArrowWindow();
            }
        }

        private string[] tooltipText = {
            "点击生成准星后，修改实时生效。",
            "有定制需求，欢迎联系作者",
            "谢谢usernano捐赠的100元"
        };
        private int tooltipIndex = 0;
        private void toolStripTimer_Tick(object sender, EventArgs e)
        {
            this.UpdateToolTip();
        }
        private void UpdateToolTip()
        {
            if (tooltipIndex >= tooltipText.Length)
            {
                tooltipIndex = 0;
            }
            this.toolStripLabel.Text = tooltipText[tooltipIndex];
            tooltipIndex++;
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.OpenUrl(UrlQQ);
        }
    }
}
